package com.heitaoking.blog.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.heitaoking.blog.dao.UserDao;
import com.heitaoking.blog.entity.User;

/**
 * 博客信息查询Servlet类
 * 
 */
public class UserQueryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	// 学生Dao对象
	UserDao dao = null;
	@Override
	public void init() throws ServletException {
		// 初始化博客Dao对象
		dao = new UserDao();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@SuppressWarnings("unused")
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 创建博客数据视图对象
		User userParams = new User();

		// 从页面获取查询条件
		// request.getParameter("");
		// TODO 此处忽略查询条件,待完成

		try {
			// 调用dao中查询博客信息列表方法,获得用户对象的集合
			List<User> userList = dao.queryUser(userParams);

			// 将查询结果保存到request对象中
			request.setAttribute("userList", userList);

			// 通过ServletContext对象获取RequestDispatcher对象
			// 通过ServletContext对象获得的路径是 相对Web应用根的路径,即绝对路径.
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/user/userList.jsp");

			// 请求转发给信息列表页面(/jsp/student/studentList.jsp)
			rd.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
